<?php

namespace App\Http\Controllers\Home;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;

use Auth;
use  Markdown;

class BlogController extends Controller
{
    /**
     * 首页视图   /b   ->index()
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
       /* $blogs = DB::table('blogs')->get();*/

       $blogs = DB::table('blogs')->paginate(2);
       //添加分页
       
       //检测用户是否登录
       $user = Auth::user();
        //var_dump($user);
       
        return view('blog.index',['blogs'=>$blogs,'user'=>$user]);
    }

    /**
     * 展现添加文章的视图   /b/create   ->create()
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('blog.mk_add');
    }

    /**
     * //保存到数据库  post  /b  ->store()
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $title = $request->title;
        $content = $request->content;

        //验证
          $this->validate($request,[
            'title'=>'required|max:50',
            'content'=>'required|max:500',
            ]);
          $data = ['title'=> $title,'content'=>$content];
          DB::table('blogs')->insert($data);
          return redirect()->route('b.index');

    }

    /**
     * 展示文章详情  /b/id  ->show($id) 方法
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        $blog = DB::table('blogs')->where('id',$id)->first();
       
        //mk  to  html 
        $blog->content = Markdown::convertToHtml($blog->content);


        return view('blog.show',['blog'=>$blog]);


    }

    /**
     * mkdrown 阅览
     * @param  Request $request [description]
     * @return [type]           [description]
     */
    public function pre_mk(Request $request)
    {
        return  Markdown::convertToHtml($request->cont);
    }

    /**
     * 修改文章
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
         $blog = DB::table('blogs')->where('id',$id)->first();
        
        return view('blog.edit',['blog'=>$blog]);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
         $this->validate($request, [
            'title' => 'required|max:50',
            'content'=>'required|max:500',
        ]);

         DB::table('blogs')->where('id',$id)->update(['title'=>$request->title,'content'=>$request->content]);
         return redirect()->route('b.index');

    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        DB::table('blogs')->where('id',$id)->delete();
        return redirect()->route('b.index');
    }
}
